package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.constant.MvcResult;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.service.IDictService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.ContestRule;
import com.stylefeng.guns.modular.system.service.IContestRuleService;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.stylefeng.guns.common.constant.Const;

import java.util.*;

/**
 * ContestRule控制器
 *
 * @author fengshuonan
 * @Date 2018-01-08 22:55:22
 */
@Controller
@RequestMapping("/contestRule")
public class ContestRuleController extends BaseController {

    private String PREFIX = "/system/integral/contestRule/";

    @Autowired
    private IContestRuleService contestRuleService;

    @Autowired
    private IDictService dictService;
    /**
     * 跳转到部门管理首页
     */
    @RequestMapping("index")
    public String index() {
        return PREFIX + "contestRule.html";
    }

    /**
     * 跳转到竞赛调考积分：国网公司级以上级别
     * @param model
     * @return
     */
    @RequestMapping(value = "stateGrid")
    public String contestStateGrid(Model model){

        return PREFIX + "contest_stateGrid.html";
    }

    /**
     * 跳转到竞赛调考积分：省公司级别
     * @param model
     * @return
     */
    @RequestMapping(value = "province")
    public String contestProvince(Model model){

        return PREFIX + "contest_province.html";
    }


    /**
     * 跳转到竞赛调考积分：地市公司级别
     * @param model
     * @return
     */
    @RequestMapping(value = "city")
    public String contestCity(Model model){

        return PREFIX + "contest_city.html";
    }

    /**
     * 跳转到添加ContestRule
     */
    @RequestMapping("/contestRule_add")
    public String contestRuleAdd(Model model) {

        Map<String,Object> condition = new HashMap<>();
        //获取公司级别
        condition.put(Dict.codeName, Const.DICT_COMPANY_TYPE);
        List<Dict> companyLevels = dictService.getChildByMap(condition);

        //获取竞赛类型
        condition.clear();
        condition.put(Dict.codeName, Const.DICT_CONTEST_TYPE);
        List<Dict> contestTypes = dictService.getChildByMap(condition);

        //获取获奖类型
        condition.clear();
        condition.put(Dict.codeName, Const.DICT_WIN_TYPE);
        List<Dict> winTypes = dictService.getChildByMap(condition);

        model.addAttribute("companyLevels",companyLevels);
        model.addAttribute("companyLevel",companyLevels.get(0).getName());

        model.addAttribute("contestTypes",contestTypes);
        model.addAttribute("contestType",contestTypes.get(0).getName());

        model.addAttribute("winTypes",winTypes);
        model.addAttribute("winType",winTypes.get(0).getName());

        return PREFIX + "contestRule_add.html";
    }

    /**
     * 跳转到修改ContestRule
     */
    @RequestMapping("/contestRule_update/{contestRuleId}")
    public String contestRuleUpdate(@PathVariable String contestRuleId, Model model) {
        ContestRule contestRule = contestRuleService.selectById(contestRuleId);
        model.addAttribute("item",contestRule);
        LogObjectHolder.me().set(contestRule);
        return PREFIX + "contestRule_edit.html";
    }

    /**
     * 获取ContestRule列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return contestRuleService.selectList(null);
    }

    /**
     * 新增ContestRule
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ContestRule contestRule) {
        contestRuleService.insert(contestRule);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除ContestRule
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String contestRuleId) {
        contestRuleService.deleteById(contestRuleId);
        return SUCCESS_TIP;
    }

    /**
     * 修改ContestRule
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ContestRule contestRule) {
        contestRuleService.updateById(contestRule);
        return super.SUCCESS_TIP;
    }

    /**
     * ContestRule详情
     */
    @RequestMapping(value = "/detail/{contestRuleId}")
    @ResponseBody
    public Object detail(@PathVariable("contestRuleId") String contestRuleId) {
        return contestRuleService.selectById(contestRuleId);
    }


    /**
     * 跳转到导入ContestRule
     */
    @RequestMapping("/contestRule_import")
    public String contestRuleImport() {
        return PREFIX + "contestRule_import.html";
    }



    @RequestMapping("/contestLevelRule")
    @ResponseBody
    public MvcResult getContestLevelRule(String companyLevelCode){
        List<ContestRule> contestRuleList = contestRuleService.getContestRules(companyLevelCode);
        MvcResult mvcResult = new MvcResult();
        mvcResult.setData(contestRuleList);

        return mvcResult;
    }



    @RequestMapping("/winLevelRule")
    @ResponseBody
    public MvcResult getWinLevelRule(String companyLevelCode, String contestLevelCode){
        List<ContestRule> winRuleList = contestRuleService.getWinRules(companyLevelCode,contestLevelCode);
        MvcResult mvcResult = new MvcResult();
        Map<String,Object> result = new HashMap<>();

        result.put("winRuleList",winRuleList);
//        if(winRuleList != null && winRuleList.size() > 0){
//            result.put("integral", winRuleList.get(0).getIntegral());
//        }else{
//            result.put("integral",0);
//        }

        mvcResult.setData(result);

        return mvcResult;
    }


    @RequestMapping("/integralRule")
    @ResponseBody
    public MvcResult getIntegralRule(String companyLevelCode, String contestLevelCode, String winLevelCode){
        Double integral = contestRuleService.getIntegral(companyLevelCode,contestLevelCode, winLevelCode);
        MvcResult mvcResult = new MvcResult();
        Map<String,Object> result = new HashMap<>();

        result.put("integral",integral);
//        if(winRuleList != null && winRuleList.size() > 0){
//            result.put("integral", winRuleList.get(0).getIntegral());
//        }else{
//            result.put("integral",0);
//        }

        mvcResult.setData(result);

        return mvcResult;
    }

}
